package tw.com.book.core.task01;

import tw.com.book.api.task01.Task01Service;
import tw.com.book.componet.DBHelper;
import tw.com.book.componet.IssueBussinessException;
import tw.com.book.componet.Msg;
import tw.com.book.domain.V001Type;
import tw.com.book.domain.V002Type;
import tw.com.book.domain.dao.V001DAO;
import tw.com.book.domain.dao.V002DAO;
import tw.com.book.dto.Task01DTO;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.parse.ParseObject;

public class Task01ServiceImpl implements Task01Service {

	private static final String TAG = Task01ServiceImpl.class
			.getCanonicalName();

	@Override
	public void crateIssue(Task01DTO dto, Context context)
			throws IssueBussinessException {
		// TODO Auto-generated method stub

		final DBHelper help = new DBHelper(context);

		final SQLiteDatabase conn = help.getWritableDatabase();
		
		
		try {
			conn.beginTransaction();
			final V001DAO v001DAO = new V001DAO(conn, context);

			final V001Type type = new V001Type();
			type.setCreateDate(dto.getNowDate());
			type.setCreateTime(dto.getNowTime());
			type.setCreateUser(dto.getCreateUser());
			type.setStatus(dto.getStatus());
			type.setUuid(dto.getUuid());
			type.setTitle(dto.getTitle());
			
		
			v001DAO.insert(type);

			final V002DAO v002DAO = new V002DAO(conn, context);

			final V002Type v002Type = new V002Type();
			v002Type.setContent(dto.getContent());
			v002Type.setModifyDate(dto.getNowDate());
			v002Type.setModifyTime(dto.getNowTime());
			v002Type.setSerialNo("001");
			v002Type.setUuid(dto.getUuid());
			v002DAO.insert(v002Type);

			conn.setTransactionSuccessful();

		} catch (IssueBussinessException e) {
			e.printStackTrace();
			throw e;
		}

		catch (Exception e) {
			Log.v(TAG, e.getMessage());
			throw new IssueBussinessException(Msg.E001,"新增失敗");
		} finally {
			conn.endTransaction();
			conn.close();
		}

	}

}
